<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>usr_28 - Vim Documentation</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim8.0">
<meta name="syntax" content="help">
<meta name="settings" content="no_pre,use_css,expand_tabs">
<link rel="stylesheet" href="style.css" type="text/css" />

<script src="jquery.min.js" type="text/javascript"></script>
<script src="mark-current-page.js" type="text/javascript"></script>
</head>

<body>

<header>

<div class="header">
  <a href="http://vim-jp.org/">vim-jp</a>
  / <a href="http://vim-jp.org/vimdoc-en/">vimdoc-en</a>
  / usr_28<br />
  <a name="top"></a><h1>usr_28 - Vim Documentation</h1>
  <a href="index.html">Return to main</a>

  <span class="EnglishJapaneseLink">
    <span class="CurrentLanguage">English</span>
  </span>
</div>
</header>

<nav>
<dl>

<dt>BASIC</dt>
<dd><ul>
<li><a href="quickref.html">quickref</a></li>
<li><a href="sponsor.html">sponsor</a></li>
</ul></dd>

<dt>USER MANUAL</dt>
<dd><ul>
<li><a href="usr_toc.html">usr_toc</a></li>
</ul></dd>

<dt>Getting Started</dt>
<dd><ul>
<li><a href="usr_01.html">usr_01</a></li>
<li><a href="usr_02.html">usr_02</a></li>
<li><a href="usr_03.html">usr_03</a></li>
<li><a href="usr_04.html">usr_04</a></li>
<li><a href="usr_05.html">usr_05</a></li>
<li><a href="usr_06.html">usr_06</a></li>
<li><a href="usr_07.html">usr_07</a></li>
<li><a href="usr_08.html">usr_08</a></li>
<li><a href="usr_09.html">usr_09</a></li>
<li><a href="usr_10.html">usr_10</a></li>
<li><a href="usr_11.html">usr_11</a></li>
<li><a href="usr_12.html">usr_12</a></li>
</ul></dd>

<dt>Editing Effectively</dt>
<dd><ul>
<li><a href="usr_20.html">usr_20</a></li>
<li><a href="usr_21.html">usr_21</a></li>
<li><a href="usr_22.html">usr_22</a></li>
<li><a href="usr_23.html">usr_23</a></li>
<li><a href="usr_24.html">usr_24</a></li>
<li><a href="usr_25.html">usr_25</a></li>
<li><a href="usr_26.html">usr_26</a></li>
<li><a href="usr_27.html">usr_27</a></li>
<li><a href="usr_28.html">usr_28</a></li>
<li><a href="usr_29.html">usr_29</a></li>
<li><a href="usr_30.html">usr_30</a></li>
<li><a href="usr_31.html">usr_31</a></li>
<li><a href="usr_32.html">usr_32</a></li>
</ul></dd>

<dt>Tuning Vim</dt>
<dd><ul>
<li><a href="usr_40.html">usr_40</a></li>
<li><a href="usr_41.html">usr_41</a></li>
<li><a href="usr_42.html">usr_42</a></li>
<li><a href="usr_43.html">usr_43</a></li>
<li><a href="usr_44.html">usr_44</a></li>
<li><a href="usr_45.html">usr_45</a></li>
</ul></dd>

<dt>Making Vim Run</dt>
<dd><ul>
<li><a href="usr_90.html">usr_90</a></li>
</ul></dd>

<dt>General subjects</dt>
<dd><ul>
<li><a href="intro.html">intro</a></li>
<li><a href="index.html">help</a></li>
<li><a href="helphelp.html">helphelp</a></li>
<li><a href="vimindex.html">index</a></li>
<li><a href="tags.html">tags</a></li>
<li><a href="howto.html">howto</a></li>
<li><a href="tips.html">tips</a></li>
<li><a href="message.html">message</a></li>
<li><a href="quotes.html">quotes</a></li>
<li><a href="todo.html">todo</a></li>
<li><a href="debug.html">debug</a></li>
<li><a href="develop.html">develop</a></li>
<li><a href="uganda.html">uganda</a></li>
</ul></dd>

<dt>Basic editing</dt>
<dd><ul>
<li><a href="starting.html">starting</a></li>
<li><a href="editing.html">editing</a></li>
<li><a href="motion.html">motion</a></li>
<li><a href="scroll.html">scroll</a></li>
<li><a href="insert.html">insert</a></li>
<li><a href="change.html">change</a></li>
<li><a href="indent.html">indent</a></li>
<li><a href="undo.html">undo</a></li>
<li><a href="repeat.html">repeat</a></li>
<li><a href="visual.html">visual</a></li>
<li><a href="various.html">various</a></li>
<li><a href="recover.html">recover</a></li>
</ul></dd>

<dt>Advanced editing</dt>
<dd><ul>
<li><a href="cmdline.html">cmdline</a></li>
<li><a href="options.html">options</a></li>
<li><a href="pattern.html">pattern</a></li>
<li><a href="map.html">map</a></li>
<li><a href="tagsrch.html">tagsrch</a></li>
<li><a href="quickfix.html">quickfix</a></li>
<li><a href="windows.html">windows</a></li>
<li><a href="tabpage.html">tabpage</a></li>
<li><a href="syntax.html">syntax</a></li>
<li><a href="spell.html">spell</a></li>
<li><a href="diff.html">diff</a></li>
<li><a href="autocmd.html">autocmd</a></li>
<li><a href="filetype.html">filetype</a></li>
<li><a href="eval.html">eval</a></li>
<li><a href="channel.html">channel</a></li>
<li><a href="fold.html">fold</a></li>
</ul></dd>

<dt>Special issues</dt>
<dd><ul>
<li><a href="print.html">print</a></li>
<li><a href="remote.html">remote</a></li>
<li><a href="term.html">term</a></li>
<li><a href="digraph.html">digraph</a></li>
<li><a href="mbyte.html">mbyte</a></li>
<li><a href="mlang.html">mlang</a></li>
<li><a href="arabic.html">arabic</a></li>
<li><a href="farsi.html">farsi</a></li>
<li><a href="hebrew.html">hebrew</a></li>
<li><a href="russian.html">russian</a></li>
<li><a href="ft_ada.html">ft_ada</a></li>
<li><a href="ft_sql.html">ft_sql</a></li>
<li><a href="hangulin.html">hangulin</a></li>
<li><a href="rileft.html">rileft</a></li>
</ul></dd>

<dt>GUI</dt>
<dd><ul>
<li><a href="gui.html">gui</a></li>
<li><a href="gui_w32.html">gui_w32</a></li>
<li><a href="gui_x11.html">gui_x11</a></li>
</ul></dd>

<dt>Interfaces</dt>
<dd><ul>
<li><a href="if_cscop.html">if_cscop</a></li>
<li><a href="if_lua.html">if_lua</a></li>
<li><a href="if_mzsch.html">if_mzsch</a></li>
<li><a href="if_perl.html">if_perl</a></li>
<li><a href="if_pyth.html">if_pyth</a></li>
<li><a href="if_tcl.html">if_tcl</a></li>
<li><a href="if_ole.html">if_ole</a></li>
<li><a href="if_ruby.html">if_ruby</a></li>
<li><a href="debugger.html">debugger</a></li>
<li><a href="workshop.html">workshop</a></li>
<li><a href="netbeans.html">netbeans</a></li>
<li><a href="sign.html">sign</a></li>
</ul></dd>

<dt>Versions</dt>
<dd><ul>
<li><a href="vi_diff.html">vi_diff</a></li>
<li><a href="version4.html">version4</a></li>
<li><a href="version5.html">version5</a></li>
<li><a href="version6.html">version6</a></li>
<li><a href="version7.html">version7</a></li>
<li><a href="version8.html">version8</a></li>
</ul></dd>

<dt>Remarks about specific systems</dt>
<dd><ul>
<li><a href="os_390.html">os_390</a></li>
<li><a href="os_amiga.html">os_amiga</a></li>
<li><a href="os_beos.html">os_beos</a></li>
<li><a href="os_dos.html">os_dos</a></li>
<li><a href="os_mac.html">os_mac</a></li>
<li><a href="os_mint.html">os_mint</a></li>
<li><a href="os_msdos.html">os_msdos</a></li>
<li><a href="os_os2.html">os_os2</a></li>
<li><a href="os_qnx.html">os_qnx</a></li>
<li><a href="os_risc.html">os_risc</a></li>
<li><a href="os_unix.html">os_unix</a></li>
<li><a href="os_vms.html">os_vms</a></li>
<li><a href="os_win32.html">os_win32</a></li>
</ul></dd>

<dt>Standard plugins</dt>
<dd><ul>
<li><a href="pi_getscript.html">pi_getscript</a></li>
<li><a href="pi_gzip.html">pi_gzip</a></li>
<li><a href="pi_logipat.html">pi_logipat</a></li>
<li><a href="pi_netrw.html">pi_netrw</a></li>
<li><a href="pi_paren.html">pi_paren</a></li>
<li><a href="pi_tar.html">pi_tar</a></li>
<li><a href="pi_vimball.html">pi_vimball</a></li>
<li><a href="pi_zip.html">pi_zip</a></li>
</ul></dd>

<dt>Filetype plugins</dt>
<dd><ul>
<li><a href="pi_spec.html">pi_spec</a></li>
</ul></dd>

<dt>Others</dt>
<dd><ul>
<li><a href="vim_faq.html">vim_faq</a></li>
</ul></dd>

</dl>
</nav>

<article class="Vimdoc VimdocJa">
<div id='vimCodeElement'>
<a class="Constant" href="usr_28.html" name="usr_28.txt">usr_28.txt</a>&nbsp;&nbsp;&nbsp;&nbsp;For&nbsp;<span class="Identifier">Vim version 8.0.</span>&nbsp;&nbsp;Last change: 2008 Jun 14<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VIM USER MANUAL - by Bram Moolenaar<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Folding<br>
<br>
<br>
Structured text can be separated in sections.&nbsp;&nbsp;And sections in sub-sections.<br>
Folding allows you to display a section as one line, providing an overview.<br>
This chapter explains the different ways this can be done.<br>
<br>
<a class="Identifier" href="usr_28.html#28.1">28.1</a>&nbsp;&nbsp;What is folding?<br>
<a class="Identifier" href="usr_28.html#28.2">28.2</a>&nbsp;&nbsp;Manual folding<br>
<a class="Identifier" href="usr_28.html#28.3">28.3</a>&nbsp;&nbsp;Working with folds<br>
<a class="Identifier" href="usr_28.html#28.4">28.4</a>&nbsp;&nbsp;Saving and restoring folds<br>
<a class="Identifier" href="usr_28.html#28.5">28.5</a>&nbsp;&nbsp;Folding by indent<br>
<a class="Identifier" href="usr_28.html#28.6">28.6</a>&nbsp;&nbsp;Folding with markers<br>
<a class="Identifier" href="usr_28.html#28.7">28.7</a>&nbsp;&nbsp;Folding by syntax<br>
<a class="Identifier" href="usr_28.html#28.8">28.8</a>&nbsp;&nbsp;Folding by expression<br>
<a class="Identifier" href="usr_28.html#28.9">28.9</a>&nbsp;&nbsp;Folding unchanged lines<br>
<a class="Identifier" href="usr_28.html#28.10">28.10</a>&nbsp;Which fold method to use?<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; Next chapter:&nbsp;<a class="Identifier" href="usr_29.html">usr_29.txt</a>&nbsp;&nbsp;Moving through programs<br>
&nbsp;Previous chapter:&nbsp;<a class="Identifier" href="usr_27.html">usr_27.txt</a>&nbsp;&nbsp;Search commands and patterns<br>
Table of contents:&nbsp;<a class="Identifier" href="usr_toc.html">usr_toc.txt</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.1" name="28.1">28.1</a>&nbsp;&nbsp;What is folding?<br>
<br>
Folding is used to show a range of lines in the buffer as a single line on the<br>
screen.&nbsp;&nbsp;Like a piece of paper which is folded to make it shorter:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+------------------------+<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|_______________________ |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \________________________\<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; / folded lines&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/________________________/<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| line 14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+------------------------+<br>
<br>
The text is still in the buffer, unchanged.&nbsp;&nbsp;Only the way lines are displayed<br>
is affected by folding.<br>
<br>
The advantage of folding is that you can get a better overview of the<br>
structure of text, by folding lines of a section and replacing it with a line<br>
that indicates that there is a section.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.2" name="28.2">28.2</a>&nbsp;&nbsp;Manual folding<br>
<br>
Try it out: Position the cursor in a paragraph and type:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zfap</div>
<br>
You will see that the paragraph is replaced by a highlighted line.&nbsp;&nbsp;You have<br>
created a fold.&nbsp;&nbsp;<a class="Identifier" href="fold.html#zf">zf</a>&nbsp;is an operator and&nbsp;<a class="Identifier" href="motion.html#ap">ap</a>&nbsp;a text object selection.&nbsp;&nbsp;You<br>
can use the&nbsp;<a class="Identifier" href="fold.html#zf">zf</a>&nbsp;operator with any movement command to create a fold for the<br>
text that it moved over.&nbsp;&nbsp;<a class="Identifier" href="fold.html#zf">zf</a>&nbsp;also works in Visual mode.<br>
<br>
To view the text again, open the fold by typing:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zo</div>
<br>
And you can close the fold again with:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zc</div>
<br>
All the folding commands start with &quot;z&quot;.&nbsp;&nbsp;With some fantasy, this looks like a<br>
folded piece of paper, seen from the side.&nbsp;&nbsp;The letter after the &quot;z&quot; has a<br>
mnemonic meaning to make it easier to remember the commands:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F-old creation<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;O-pen a fold<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C-lose a fold<br>
<br>
Folds can be nested: A region of text that contains folds can be folded<br>
again.&nbsp;&nbsp;For example, you can fold each paragraph in this section, and then<br>
fold all the sections in this chapter.&nbsp;&nbsp;Try it out.&nbsp;&nbsp;You will notice that<br>
opening the fold for the whole chapter will restore the nested folds as they<br>
were, some may be open and some may be closed.<br>
<br>
Suppose you have created several folds, and now want to view all the text.<br>
You could go to each fold and type &quot;zo&quot;.&nbsp;&nbsp;To do this faster, use this command:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zr</div>
<br>
This will R-educe the folding.&nbsp;&nbsp;The opposite is:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zm</div>
<br>
This folds M-ore.&nbsp;&nbsp;You can repeat &quot;zr&quot; and &quot;zm&quot; to open and close nested folds<br>
of several levels.<br>
<br>
If you have nested several levels deep, you can open all of them with:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zR</div>
<br>
This R-educes folds until there are none left.&nbsp;&nbsp;And you can close all folds<br>
with:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zM</div>
<br>
This folds M-ore and M-ore.<br>
<br>
You can quickly disable the folding with the&nbsp;<a class="Identifier" href="fold.html#zn">zn</a>&nbsp;command.&nbsp;&nbsp;Then&nbsp;<a class="Identifier" href="fold.html#zN">zN</a>&nbsp;brings<br>
back the folding as it was.&nbsp;&nbsp;<a class="Identifier" href="fold.html#zi">zi</a>&nbsp;toggles between the two.&nbsp;&nbsp;This is a useful<br>
way of working:<br>
- create folds to get overview on your file<br>
- move around to where you want to do your work<br>
- do&nbsp;<a class="Identifier" href="fold.html#zi">zi</a>&nbsp;to look at the text and edit it<br>
- do&nbsp;<a class="Identifier" href="fold.html#zi">zi</a>&nbsp;again to go back to moving around<br>
<br>
More about manual folding in the reference manual:&nbsp;<a class="Identifier" href="fold.html#fold-manual">fold-manual</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.3" name="28.3">28.3</a>&nbsp;&nbsp;Working with folds<br>
<br>
When some folds are closed, movement commands like &quot;j&quot; and &quot;k&quot; move over a<br>
fold like it was a single, empty line.&nbsp;&nbsp;This allows you to quickly move around<br>
over folded text.<br>
<br>
You can yank, delete and put folds as if it was a single line.&nbsp;&nbsp;This is very<br>
useful if you want to reorder functions in a program.&nbsp;&nbsp;First make sure that<br>
each fold contains a whole function (or a bit less) by selecting the right<br>
<a class="Type" href="options.html#'foldmethod'">'foldmethod'</a>.&nbsp;&nbsp;Then delete the function with &quot;dd&quot;, move the cursor and put it<br>
with &quot;p&quot;.&nbsp;&nbsp;If some lines of the function are above or below the fold, you can<br>
use Visual selection:<br>
- put the cursor on the first line to be moved<br>
- hit &quot;V&quot; to start Visual mode<br>
- put the cursor on the last line to be moved<br>
- hit &quot;d&quot; to delete the selected lines.<br>
- move the cursor to the new position and &quot;p&quot;ut the lines there.<br>
<br>
It is sometimes difficult to see or remember where a fold is located, thus<br>
where a&nbsp;<a class="Identifier" href="fold.html#zo">zo</a>&nbsp;command would actually work.&nbsp;&nbsp;To see the defined folds:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldcolumn=4</div>
<br>
This will show a small column on the left of the window to indicate folds.<br>
A &quot;+&quot; is shown for a closed fold.&nbsp;&nbsp;A &quot;-&quot; is shown at the start of each open<br>
fold and &quot;|&quot; at following lines of the fold.<br>
<br>
You can use the mouse to open a fold by clicking on the &quot;+&quot; in the foldcolumn.<br>
Clicking on the &quot;-&quot; or a &quot;|&quot; below it will close an open fold.<br>
<br>
To open all folds at the cursor line use&nbsp;<a class="Identifier" href="fold.html#zO">zO</a>.<br>
To close all folds at the cursor line use&nbsp;<a class="Identifier" href="fold.html#zC">zC</a>.<br>
To delete a fold at the cursor line use&nbsp;<a class="Identifier" href="fold.html#zd">zd</a>.<br>
To delete all folds at the cursor line use&nbsp;<a class="Identifier" href="fold.html#zD">zD</a>.<br>
<br>
When in Insert mode, the fold at the cursor line is never closed.&nbsp;&nbsp;That allows<br>
you to see what you type!<br>
<br>
Folds are opened automatically when jumping around or moving the cursor left<br>
or right.&nbsp;&nbsp;For example, the &quot;0&quot; command opens the fold under the cursor<br>
(if&nbsp;<a class="Type" href="options.html#'foldopen'">'foldopen'</a>&nbsp;contains &quot;hor&quot;, which is the default).&nbsp;&nbsp;The&nbsp;<a class="Type" href="options.html#'foldopen'">'foldopen'</a>&nbsp;option<br>
can be changed to open folds for specific commands.&nbsp;&nbsp;If you want the line<br>
under the cursor always to be open, do this:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldopen=all</div>
<br>
Warning: You won't be able to move onto a closed fold then.&nbsp;&nbsp;You might want to<br>
use this only temporarily and then set it back to the default:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldopen&amp;</div>
<br>
You can make folds close automatically when you move out of it:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldclose=all</div>
<br>
This will re-apply&nbsp;<a class="Type" href="options.html#'foldlevel'">'foldlevel'</a>&nbsp;to all folds that don't contain the cursor.<br>
You have to try it out if you like how this feels.&nbsp;&nbsp;Use&nbsp;<a class="Identifier" href="fold.html#zm">zm</a>&nbsp;to fold more and<br>
<a class="Identifier" href="fold.html#zr">zr</a>&nbsp;to fold less (reduce folds).<br>
<br>
The folding is local to the window.&nbsp;&nbsp;This allows you to open two windows on<br>
the same buffer, one with folds and one without folds.&nbsp;&nbsp;Or one with all folds<br>
closed and one with all folds open.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.4" name="28.4">28.4</a>&nbsp;&nbsp;Saving and restoring folds<br>
<br>
When you abandon a file (starting to edit another one), the state of the folds<br>
is lost.&nbsp;&nbsp;If you come back to the same file later, all manually opened and<br>
closed folds are back to their default.&nbsp;&nbsp;When folds have been created<br>
manually, all folds are gone!&nbsp;&nbsp;To save the folds use the&nbsp;<a class="Identifier" href="starting.html#:mkview">:mkview</a>&nbsp;command:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:mkview</div>
<br>
This will store the settings and other things that influence the view on the<br>
file.&nbsp;&nbsp;You can change what is stored with the&nbsp;<a class="Type" href="options.html#'viewoptions'">'viewoptions'</a>&nbsp;option.<br>
When you come back to the same file later, you can load the view again:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:loadview</div>
<br>
You can store up to ten views on one file.&nbsp;&nbsp;For example, to save the current<br>
setup as the third view and load the second view:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:mkview 3<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:loadview 2</div>
<br>
<span class="Todo">Note</span>&nbsp;that when you insert or delete lines the views might become invalid.<br>
Also check out the&nbsp;<a class="Type" href="options.html#'viewdir'">'viewdir'</a>&nbsp;option, which specifies where the views are<br>
stored.&nbsp;&nbsp;You might want to delete old views now and then.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.5" name="28.5">28.5</a>&nbsp;&nbsp;Folding by indent<br>
<br>
Defining folds with&nbsp;<a class="Identifier" href="fold.html#zf">zf</a>&nbsp;is a lot of work.&nbsp;&nbsp;If your text is structured by<br>
giving lower level items a larger indent, you can use the indent folding<br>
method.&nbsp;&nbsp;This will create folds for every sequence of lines with the same<br>
indent.&nbsp;&nbsp;Lines with a larger indent will become nested folds.&nbsp;&nbsp;This works well<br>
with many programming languages.<br>
<br>
Try this by setting the&nbsp;<a class="Type" href="options.html#'foldmethod'">'foldmethod'</a>&nbsp;option:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldmethod=indent</div>
<br>
Then you can use the&nbsp;<a class="Identifier" href="fold.html#zm">zm</a>&nbsp;and&nbsp;<a class="Identifier" href="fold.html#zr">zr</a>&nbsp;commands to fold more and reduce folding.<br>
It's easy to see on this example text:<br>
<br>
This line is not indented<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented once<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented twice<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented twice<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented once<br>
This line is not indented<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented once<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This line is indented once<br>
<br>
<span class="Todo">Note</span>&nbsp;that the relation between the amount of indent and the fold depth depends<br>
on the&nbsp;<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a>&nbsp;option.&nbsp;&nbsp;Each&nbsp;<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a>&nbsp;worth of indent adds one to the<br>
depth of the fold.&nbsp;&nbsp;This is called a fold level.<br>
<br>
When you use the&nbsp;<a class="Identifier" href="fold.html#zr">zr</a>&nbsp;and&nbsp;<a class="Identifier" href="fold.html#zm">zm</a>&nbsp;commands you actually increase or decrease the<br>
<a class="Type" href="options.html#'foldlevel'">'foldlevel'</a>&nbsp;option.&nbsp;&nbsp;You could also set it directly:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldlevel=3</div>
<br>
This means that all folds with three times a&nbsp;<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a>&nbsp;indent or more will<br>
be closed.&nbsp;&nbsp;The lower the foldlevel, the more folds will be closed.&nbsp;&nbsp;When<br>
<a class="Type" href="options.html#'foldlevel'">'foldlevel'</a>&nbsp;is zero, all folds are closed.&nbsp;&nbsp;<a class="Identifier" href="fold.html#zM">zM</a>&nbsp;does set&nbsp;<a class="Type" href="options.html#'foldlevel'">'foldlevel'</a>&nbsp;to zero.<br>
The opposite command&nbsp;<a class="Identifier" href="fold.html#zR">zR</a>&nbsp;sets&nbsp;<a class="Type" href="options.html#'foldlevel'">'foldlevel'</a>&nbsp;to the deepest fold level that is<br>
present in the file.<br>
<br>
Thus there are two ways to open and close the folds:<br>
(A) By setting the fold level.<br>
&nbsp;&nbsp;&nbsp;&nbsp;This gives a very quick way of &quot;zooming out&quot; to view the structure of the<br>
&nbsp;&nbsp;&nbsp;&nbsp;text, move the cursor, and &quot;zoom in&quot; on the text again.<br>
<br>
(B) By using&nbsp;<a class="Identifier" href="fold.html#zo">zo</a>&nbsp;and&nbsp;<a class="Identifier" href="fold.html#zc">zc</a>&nbsp;commands to open or close specific folds.<br>
&nbsp;&nbsp;&nbsp;&nbsp;This allows opening only those folds that you want to be open, while other<br>
&nbsp;&nbsp;&nbsp;&nbsp;folds remain closed.<br>
<br>
This can be combined: You can first close most folds by using&nbsp;<a class="Identifier" href="fold.html#zm">zm</a>&nbsp;a few times<br>
and then open a specific fold with&nbsp;<a class="Identifier" href="fold.html#zo">zo</a>.&nbsp;&nbsp;Or open all folds with&nbsp;<a class="Identifier" href="fold.html#zR">zR</a>&nbsp;and<br>
then close specific folds with&nbsp;<a class="Identifier" href="fold.html#zc">zc</a>.<br>
<br>
But you cannot manually define folds when&nbsp;<a class="Type" href="options.html#'foldmethod'">'foldmethod'</a>&nbsp;is &quot;indent&quot;, as that<br>
would conflict with the relation between the indent and the fold level.<br>
<br>
More about folding by indent in the reference manual:&nbsp;<a class="Identifier" href="fold.html#fold-indent">fold-indent</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.6" name="28.6">28.6</a>&nbsp;&nbsp;Folding with markers<br>
<br>
Markers in the text are used to specify the start and end of a fold region.<br>
This gives precise control over which lines are included in a fold.&nbsp;&nbsp;The<br>
disadvantage is that the text needs to be modified.<br>
<br>
Try it:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldmethod=marker</div>
<br>
Example text, as it could appear in a C program:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* foobar () {{ "{{{" }} */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int foobar()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* return a value {{ "{{{" }} */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 42;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* }}} */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* }}} */<br>
<br>
Notice that the folded line will display the text before the marker.&nbsp;&nbsp;This is<br>
very useful to tell what the fold contains.<br>
<br>
It's quite annoying when the markers don't pair up correctly after moving some<br>
lines around.&nbsp;&nbsp;This can be avoided by using numbered markers.&nbsp;&nbsp;Example:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* global variables {{ "{{{" }}1 */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int varA, varB;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* functions {{ "{{{" }}1 */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* funcA() {{ "{{{" }}2 */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void funcA() {}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* funcB() {{ "{{{" }}2 */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void funcB() {}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* }}}1 */<br>
<br>
At every numbered marker a fold at the specified level begins.&nbsp;&nbsp;This will make<br>
any fold at a higher level stop here.&nbsp;&nbsp;You can just use numbered start markers<br>
to define all folds.&nbsp;&nbsp;Only when you want to explicitly stop a fold before<br>
another starts you need to add an end marker.<br>
<br>
More about folding with markers in the reference manual:&nbsp;<a class="Identifier" href="fold.html#fold-marker">fold-marker</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.7" name="28.7">28.7</a>&nbsp;&nbsp;Folding by syntax<br>
<br>
For each language Vim uses a different syntax file.&nbsp;&nbsp;This defines the colors<br>
for various items in the file.&nbsp;&nbsp;If you are reading this in Vim, in a terminal<br>
that supports colors, the colors you see are made with the &quot;help&quot; syntax file.<br>
&nbsp;&nbsp; In the syntax files it is possible to add syntax items that have the &quot;fold&quot;<br>
argument.&nbsp;&nbsp;These define a fold region.&nbsp;&nbsp;This requires writing a syntax file<br>
and adding these items in it.&nbsp;&nbsp;That's not so easy to do.&nbsp;&nbsp;But once it's done,<br>
all folding happens automatically.<br>
&nbsp;&nbsp; Here we'll assume you are using an existing syntax file.&nbsp;&nbsp;Then there is<br>
nothing more to explain.&nbsp;&nbsp;You can open and close folds as explained above.<br>
The folds will be created and deleted automatically when you edit the file.<br>
<br>
More about folding by syntax in the reference manual:&nbsp;<a class="Identifier" href="fold.html#fold-syntax">fold-syntax</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.8" name="28.8">28.8</a>&nbsp;&nbsp;Folding by expression<br>
<br>
This is similar to folding by indent, but instead of using the indent of a<br>
line a user function is called to compute the fold level of a line.&nbsp;&nbsp;You can<br>
use this for text where something in the text indicates which lines belong<br>
together.&nbsp;&nbsp;An example is an e-mail message where the quoted text is indicated<br>
by a &quot;&gt;&quot; before the line.&nbsp;&nbsp;To fold these quotes use this:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldmethod=expr<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldexpr=strlen(substitute(substitute(getline(v:lnum),'\\s','',\&quot;g\&quot;),'[^&gt;].*','',''))</div>
<br>
You can try it out on this text:<br>
<br>
&gt; quoted text he wrote<br>
&gt; quoted text he wrote<br>
&gt; &gt; double quoted text I wrote<br>
&gt; &gt; double quoted text I wrote<br>
<br>
Explanation for the&nbsp;<a class="Type" href="options.html#'foldexpr'">'foldexpr'</a>&nbsp;used in the example (inside out):<br>
&nbsp;&nbsp; getline(v:lnum)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gets the current line<br>
&nbsp;&nbsp; substitute(...,'\\s','','g')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removes all white space from the line<br>
&nbsp;&nbsp; substitute(...,'[^&gt;].*','','')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removes everything after leading '&gt;'s<br>
&nbsp;&nbsp; strlen(...)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counts the length of the string, which<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is the number of '&gt;'s found<br>
<br>
<span class="Todo">Note</span>&nbsp;that a backslash must be inserted before every space, double quote and<br>
backslash for the &quot;:set&quot; command.&nbsp;&nbsp;If this confuses you, do<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldexpr</div>
<br>
to check the actual resulting value.&nbsp;&nbsp;To correct a complicated expression, use<br>
the command-line completion:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:set foldexpr=&lt;Tab&gt;</div>
<br>
Where&nbsp;<span class="Special">&lt;Tab&gt;</span>&nbsp;is a real Tab.&nbsp;&nbsp;Vim will fill in the previous value, which you can<br>
then edit.<br>
<br>
When the expression gets more complicated you should put it in a function and<br>
set&nbsp;<a class="Type" href="options.html#'foldexpr'">'foldexpr'</a>&nbsp;to call that function.<br>
<br>
More about folding by expression in the reference manual:&nbsp;<a class="Identifier" href="fold.html#fold-expr">fold-expr</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.9" name="28.9">28.9</a>&nbsp;&nbsp;Folding unchanged lines<br>
<br>
This is useful when you set the&nbsp;<a class="Type" href="options.html#'diff'">'diff'</a>&nbsp;option in the same window.&nbsp;&nbsp;The<br>
<a class="Identifier" href="diff.html#vimdiff">vimdiff</a>&nbsp;command does this for you.&nbsp;&nbsp;Example:<br>
<br>
<div class="helpExample">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:setlocal diff foldmethod=diff scrollbind nowrap foldlevel=1</div>
<br>
Do this in every window that shows a different version of the same file.&nbsp;&nbsp;You<br>
will clearly see the differences between the files, while the text that didn't<br>
change is folded.<br>
<br>
For more details see&nbsp;<a class="Identifier" href="fold.html#fold-diff">fold-diff</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_28.html#28.10" name="28.10">28.10</a>&nbsp;Which fold method to use?<br>
<br>
All these possibilities make you wonder which method you should choose.<br>
Unfortunately, there is no golden rule.&nbsp;&nbsp;Here are some hints.<br>
<br>
If there is a syntax file with folding for the language you are editing, that<br>
is probably the best choice.&nbsp;&nbsp;If there isn't one, you might try to write it.<br>
This requires a good knowledge of search patterns.&nbsp;&nbsp;It's not easy, but when<br>
it's working you will not have to define folds manually.<br>
<br>
Typing commands to manually fold regions can be used for unstructured text.<br>
Then use the&nbsp;<a class="Identifier" href="starting.html#:mkview">:mkview</a>&nbsp;command to save and restore your folds.<br>
<br>
The marker method requires you to change the file.&nbsp;&nbsp;If you are sharing the<br>
files with other people or you have to meet company standards, you might not<br>
be allowed to add them.<br>
&nbsp;&nbsp; The main advantage of markers is that you can put them exactly where you<br>
want them.&nbsp;&nbsp;That avoids that a few lines are missed when you cut and paste<br>
folds.&nbsp;&nbsp;And you can add a comment about what is contained in the fold.<br>
<br>
Folding by indent is something that works in many files, but not always very<br>
well.&nbsp;&nbsp;Use it when you can't use one of the other methods.&nbsp;&nbsp;However, it is<br>
very useful for outlining.&nbsp;&nbsp;Then you specifically use one&nbsp;<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a>&nbsp;for<br>
each nesting level.<br>
<br>
Folding with expressions can make folds in almost any structured text.&nbsp;&nbsp;It is<br>
quite simple to specify, especially if the start and end of a fold can easily<br>
be recognized.<br>
&nbsp;&nbsp; If you use the &quot;expr&quot; method to define folds, but they are not exactly how<br>
you want them, you could switch to the &quot;manual&quot; method.&nbsp;&nbsp;This will not remove<br>
the defined folds.&nbsp;&nbsp;Then you can delete or add folds manually.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<br>
Next chapter:&nbsp;<a class="Identifier" href="usr_29.html">usr_29.txt</a>&nbsp;&nbsp;Moving through programs<br>
<br>
Copyright: see&nbsp;<a class="Identifier" href="usr_01.html#manual-copyright">manual-copyright</a>&nbsp;&nbsp;vim:tw=78:ts=8:ft=help:norl:<br>
</div>

</article>

<footer>
<a href="#top">Return to the top</a> - <a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
  <span class="CurrentLanguage">English</span>
</span>
<br />
<div style="text-align:right;">
Hosted by <a href="https://github.com/vim-jp/vimdoc-en">vimdoc-en project</a><br />
If you met any problem, please report it to <a href="https://github.com/vim-jp/vimdoc-en/issues">issue</a>.<br />
</div>
</footer>

<!--<script src="js/check-referrer.js" type="text/javascript"></script>-->

</body>
</html>
<!-- vim:set ts=8 sts=2 sw=2 tw=0 et: -->
